package com.herench.bs.transfer;

import com.herench.bs.transfer.Dialect;
import com.herench.bs.utils.DataFormatUtils;
import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

/**
 * com.herench.bs.transfer.transfer
 *
 * @author wangwenkai
 * @date 2017/10/30/15:47
 * E-mail：605064334@qq.com
 */
public class LoadRegistRecordsForClinicChargeTransfer extends AbstractTransfer {

    public LoadRegistRecordsForClinicChargeTransfer(String method, Client client,String messageMethod)  {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String idCard = (String) params.get("idCard");
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        dataRowElement.addAttribute("QUERY_TYPE", "2").addAttribute("QUERY_CONJ", idCard);
        dataRowElement.addAttribute("START_DATE", startTime).addAttribute("END_DATE", endTime);
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> res = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        List<Map<String, Object>> dateList = new ArrayList<>();
        if (Dialect.getSuccessfulState().equals(executeStatus)) {
            List<Element> dataRow = document.getRootElement().element("ReturnData").element("Data").elements();
            for (Element element : dataRow) {
                LoadRegistRecordsForClinicChargeInnerMassaage innerMassaage = new LoadRegistRecordsForClinicChargeInnerMassaage(element);
                String state = innerMassaage.getStatus();
                if ("1".equals(state)) {
                    //2 挂号成功未就诊
                    state = "2";
                } else if ("2".equals(state)) {
                    //3 已就诊
                    state = "3";
                } else if ("6".equals(state)) {
                    //6 退号中间状态
                    state = "6";
                } else if ("8".equals(state)) {
                    //4 挂号中间状态
                    state = "4";
                } else if ("9".equals(state)) {
                    //9 已退号
                    state = "9";
                }
                if ("2".equals(state) || "3".equals(state)) {
                    //flage=0;无重复日期
                    String flag = "0";
                    for (Map<String, Object> subDateMap : dateList) {
                        List<Map<String, Object>> resList = (List<Map<String, Object>>) subDateMap.get("resList");
                        Map<String, Object> registMap = new HashMap<>(16);
                        if (subDateMap.get("resDate").equals(innerMassaage.getVisitDate())) {
                            registMap.put("visitNo", innerMassaage.getVisitNo());
                            registMap.put("transactNo", innerMassaage.getTranNo());
                            registMap.put("deptCode", innerMassaage.getVisitDept());
                            registMap.put("deptName", innerMassaage.getVisitDeptName());
                            registMap.put("docName", innerMassaage.getDoctor());
                            registMap.put("resDate", innerMassaage.getVisitDate());
                            registMap.put("clinicLabel", innerMassaage.getClinicLabel());
                            registMap.put("hisClinicAppointsId", innerMassaage.getClinicAppointsId());
                            registMap.put("appointId", innerMassaage.getPlatformAppointId());
                            registMap.put("rcptNo", innerMassaage.getRcptNo());
                            registMap.put("name", innerMassaage.getName());
                            registMap.put("patientType", innerMassaage.getChargeType());
                            registMap.put("appointDate", innerMassaage.getVisitDate());
                            registMap.put("ampm", Dialect.getAmpmCode(innerMassaage.getVisitTimeDesc()));
                            registMap.put("doctorTitleCode", Dialect.getClinicTypeCode(innerMassaage.getClinicType()));
                            registMap.put("innerSort", innerMassaage.getSerialNo());
                            registMap.put("queueNum", innerMassaage.getSerialNo());
                            registMap.put("state", state);
                            registMap.put("clinicFee", innerMassaage.getClinicCharge());
                            registMap.put("deptAddress", innerMassaage.getDeptLocation());
                            registMap.put("totalCost", innerMassaage.getClinicCharge());
                            registMap.put("insurVisitId", innerMassaage.getInsurVisitId());
                            registMap.put("insurPay", innerMassaage.getInsurPay());
                            registMap.put("insurAccountPay", innerMassaage.getAccountPay());
                            registMap.put("ownPay", innerMassaage.getOwnPay());
                            registMap.put("cardId", innerMassaage.getCardId());
                            resList.add(registMap);
                            //flage=1;有重复日期
                            flag = "1";
                        }
                    }
                    if ("0".equals(flag)) {
                        Map<String, Object> subDateMap = new LinkedHashMap<>();
                        List<Map<String, Object>> subReglist = new ArrayList<>();
                        Map<String, Object> subRegMap = new HashMap<>(16);
                        subRegMap.put("visitNo", innerMassaage.getVisitNo());
                        subRegMap.put("resDate", innerMassaage.getVisitDate());
                        subRegMap.put("transactNo", innerMassaage.getTranNo());
                        subRegMap.put("deptCode", innerMassaage.getVisitDept());
                        subRegMap.put("deptName", innerMassaage.getVisitDeptName());
                        subRegMap.put("docName", innerMassaage.getDoctor());
                        subRegMap.put("clinicLabel", innerMassaage.getClinicLabel());
                        subRegMap.put("hisClinicAppointsId", innerMassaage.getClinicAppointsId());
                        subRegMap.put("appointId", innerMassaage.getPlatformAppointId());
                        subRegMap.put("rcptNo", innerMassaage.getRcptNo());
                        subRegMap.put("name", innerMassaage.getName());
                        subRegMap.put("patientType", innerMassaage.getChargeType());
                        subRegMap.put("appointDate", innerMassaage.getVisitDate());
                        subRegMap.put("ampm", Dialect.getAmpmCode(innerMassaage.getVisitTimeDesc()));
                        subRegMap.put("doctorTitleCode", Dialect.getClinicTypeCode(innerMassaage.getClinicType()));
                        subRegMap.put("innerSort", innerMassaage.getSerialNo());
                        subRegMap.put("queueNum", innerMassaage.getSerialNo());
                        subRegMap.put("state", state);
                        subRegMap.put("clinicFee", innerMassaage.getClinicCharge());
                        subRegMap.put("deptAddress", innerMassaage.getDeptLocation());
                        subRegMap.put("totalCost", innerMassaage.getClinicCharge());
                        subRegMap.put("insurVisitId", innerMassaage.getInsurVisitId());
                        subRegMap.put("insurPay", innerMassaage.getInsurPay());
                        subRegMap.put("insurAccountPay", innerMassaage.getAccountPay());
                        subRegMap.put("ownPay", innerMassaage.getOwnPay());
                        subRegMap.put("cardId", innerMassaage.getCardId());
                        subReglist.add(subRegMap);
                        subDateMap.put("resDate", innerMassaage.getVisitDate());
                        subDateMap.put("patientId", innerMassaage.getPatientId());
                        subDateMap.put("resList", subReglist);
                        dateList.add(subDateMap);
                    }
                }
            }

            //按照“resDate”降序排序
            Collections.sort(dateList, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Date a = null;
                    Date b = null;
                    try {
                        a = DataFormatUtils.parserDay((String) o1.get("resDate"));
                        b = DataFormatUtils.parserDay((String) o2.get("resDate"));
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("LoadRegistRecordsForClinicChargeTransfer>>>toResultMessage>>>compare:resDate 日期格式转换失败！");
                    }
                    int compareRes = a.compareTo(b);
                    return (compareRes < 0 ? 1 : (compareRes < 1 ? 0 : -1));
                }
            });
        }
        res.put("dateList", dateList);
        res.put("code", executeStatus);
        res.put("message", executeMessage);
        return res;
    }

    public class LoadRegistRecordsForClinicChargeInnerMassaage implements InnerMessage {
        /**
         * 号表名称，中文
         */
        private String clinicLabel;
        /**
         * 时段，中文
         */
        private String visitTimeDesc;
        /**
         * 患者ID
         */
        private String patientId;
        /**
         * 身份证号
         */
        private String idNo;
        /**
         * 姓名
         */
        private String name;
        /**
         * 性别
         */
        private String sex;
        /**
         * 年龄
         */
        private String age;
        /**
         * 身份，代码
         */
        private String identity;
        /**
         * 费别，代码
         */
        private String chargeType;
        /**
         * 号类，中文
         */
        private String clinicType;
        /**
         * 就诊科室，号表中科室代码
         */
        private String visitDept;
        /**
         * 医生姓名
         */
        private String doctor;
        /**
         * 挂号费
         */
        private String registFee;
        /**
         * 诊查费
         */
        private String clinicFee;
        /**
         * 其他费用
         */
        private String otherFee;
        /**
         * 总费用
         */
        private String clinicCharge;
        /**
         * 个人编码
         */
        private String personNo;
        /**
         * 就诊号
         */
        private String visitNo;
        /**
         * 就诊日期
         */
        private String visitDate;
        /**
         * 内部票据号
         */
        private String rcptNo;
        /**
         * 医保卡号
         */
        private String insurNo;
        /**
         * 医保就诊顺序号
         */
        private String insurVisitId;
        /**
         * 医保类型
         */
        private String insuranceType;
        /**
         * 支付类别
         */
        private String payType;
        /**
         * 门特类别
         */
        private String mtType;
        /**
         * 统筹支付
         */
        private String insurPay;
        /**
         * 个人账户支付
         */
        private String accountPay;
        /**
         * 个人自费
         */
        private String ownPay;
        /**
         * 合同号
         */
        private String tranNo;
        /**
         * 是否有未交费记录
         */
        private String registrationStatus;
        /**
         * 就诊地点
         */
        private String deptLocation;
        /**
         * 身份名称
         */
        private String identityName;
        /**
         * 费别名称
         */
        private String chargeTypeName;
        /**
         * 就诊科室名称
         */
        private String visitDeptName;
        /**
         * HIS预约ID
         */
        private String clinicAppointsId;
        /**
         * 凭条打印次数
         */
        private String ptPrintTimes;
        /**
         * 卡号
         */
        private String cardId;
        /**
         * 就诊序号v
         */
        private String serialNo;
        /**
         * 挂号状态: 1 挂号成功未就诊  2已就诊 6退号中间状态  8挂号中间状态  9退号状态
         */
        private String status;
        /**
         * 支付类型
         */
        private String moneyType;
        /**
         * 挂号来源
         */
        private String registSource;
        /**
         * 平台预约识别码
         */
        private String platformAppointId;

        public String getPlatformAppointId() {
            return platformAppointId;
        }

        public String getClinicLabel() {
            return clinicLabel;
        }

        public String getVisitTimeDesc() {
            return visitTimeDesc;
        }

        public String getPatientId() {
            return patientId;
        }

        public String getIdNo() {
            return idNo;
        }

        public String getName() {
            return name;
        }

        public String getSex() {
            return sex;
        }

        public String getAge() {
            return age;
        }

        public String getIdentity() {
            return identity;
        }

        public String getChargeType() {
            return chargeType;
        }

        public String getClinicType() {
            return clinicType;
        }

        public String getVisitDept() {
            return visitDept;
        }

        public String getDoctor() {
            return doctor;
        }

        public String getRegistFee() {
            return registFee;
        }

        public String getClinicFee() {
            return clinicFee;
        }

        public String getOtherFee() {
            return otherFee;
        }

        public String getClinicCharge() {
            return clinicCharge;
        }

        public String getPersonNo() {
            return personNo;
        }

        public String getVisitNo() {
            return visitNo;
        }

        public String getVisitDate() {
            return visitDate;
        }

        public String getRcptNo() {
            return rcptNo;
        }

        public String getInsurNo() {
            return insurNo;
        }

        public String getInsurVisitId() {
            return insurVisitId;
        }

        public String getInsuranceType() {
            return insuranceType;
        }

        public String getPayType() {
            return payType;
        }

        public String getMtType() {
            return mtType;
        }

        public String getInsurPay() {
            return insurPay;
        }

        public String getAccountPay() {
            return accountPay;
        }

        public String getOwnPay() {
            return ownPay;
        }

        public String getTranNo() {
            return tranNo;
        }

        public String getRegistrationStatus() {
            return registrationStatus;
        }

        public String getDeptLocation() {
            return deptLocation;
        }

        public String getIdentityName() {
            return identityName;
        }

        public String getChargeTypeName() {
            return chargeTypeName;
        }

        public String getVisitDeptName() {
            return visitDeptName;
        }

        public String getClinicAppointsId() {
            return clinicAppointsId;
        }

        public String getPtPrintTimes() {
            return ptPrintTimes;
        }

        public String getCardId() {
            return cardId;
        }

        public String getSerialNo() {
            return serialNo;
        }

        public String getStatus() {
            return status;
        }

        public String getMoneyType() {
            return moneyType;
        }

        public String getRegistSource() {
            return registSource;
        }

        public LoadRegistRecordsForClinicChargeInnerMassaage(Element element) {
            this.clinicLabel = element.attributeValue("CLINIC_LABEL");
            this.visitTimeDesc = element.attributeValue("VISIT_TIME_DESC");
            this.patientId = element.attributeValue("PATIENT_ID");
            this.idNo = element.attributeValue("ID_NO");
            this.name = element.attributeValue("NAME");
            this.sex = element.attributeValue("SEX");
            this.age = element.attributeValue("AGE");
            this.identity = element.attributeValue("IDENTITY");
            this.chargeType = element.attributeValue("CHARGE_TYPE");
            this.clinicType = element.attributeValue("CLINIC_TYPE");
            this.visitDept = element.attributeValue("VISIT_DEPT");
            this.doctor = element.attributeValue("DOCTOR");
            this.registFee = element.attributeValue("REGIST_FEE");
            this.clinicFee = element.attributeValue("CLINIC_FEE");
            this.otherFee = element.attributeValue("OTHER_FEE");
            this.clinicCharge = element.attributeValue("CLINIC_CHARGE");
            this.personNo = element.attributeValue("PERSON_NO");
            this.visitNo = element.attributeValue("VISIT_NO");
            this.visitDate = element.attributeValue("VISIT_DATE");
            this.rcptNo = element.attributeValue("RCPT_NO");
            this.insurNo = element.attributeValue("INSUR_NO");
            this.insurVisitId = element.attributeValue("INSUR_VISIT_ID");
            this.insuranceType = element.attributeValue("INSURANCE_TYPE");
            this.payType = element.attributeValue("PAY_TYPE");
            this.mtType = element.attributeValue("MT_TYPE");
            this.insurPay = element.attributeValue("INSUR_PAY");
            this.accountPay = element.attributeValue("ACCOUNT_PAY");
            this.ownPay = element.attributeValue("OWN_PAY");
            this.tranNo = element.attributeValue("TRAN_NO");
            this.registrationStatus = element.attributeValue("REGISTRATION_STATUS");
            this.deptLocation = element.attributeValue("DEPT_LOCATION");
            this.identityName = element.attributeValue("IDENTITY_NAME");
            this.chargeTypeName = element.attributeValue("CHARGE_TYPE_NAME");
            this.visitDeptName = element.attributeValue("VISIT_DEPT_NAME");
            this.clinicAppointsId = element.attributeValue("CLINIC_APPOINTS_ID");
            this.ptPrintTimes = element.attributeValue("PT_PRINT_TIMES");
            this.cardId = element.attributeValue("CARD_ID");
            this.serialNo = element.attributeValue("SERIAL_NO");
            this.status = element.attributeValue("STATUS");
            this.moneyType = element.attributeValue("MONEY_TYPE");
            this.registSource = element.attributeValue("REGIST_SOURCE");
            this.platformAppointId = element.attributeValue("PLATFORM_APPOINT_ID");
        }
    }
}
